//
//  PEMMotionCongruenceModel.swift
//  PhaseExcitedMotion
//
//  Created by iOS-Czz on 2023/11/16.
//

import UIKit
class PEMMotionCongruenceViewModel{
    var dataSource = [PEMMotionCongruenceModel]()
    var pending = ""
    var invite = ""
    var invited = ""
}
extension PEMMotionCongruenceViewModel{
    func getPageData(_ month:String, onSuccess: ((_ models: [PEMMotionCongruenceModel]) ->Void)?){
        let dataHelper = PEMMotionCongruenceModel.getUsingLKDBHelper()
        let sql = "select * from @t where month = '\(month)'"
        let modelArr = dataHelper.search(withSQL: sql, to: PEMMotionCongruenceModel.self) as! [PEMMotionCongruenceModel]
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy.MM.dd"
        let sortedModels = modelArr.sorted { (model1, model2) -> Bool in
            guard let date1 = dateFormatter.date(from: model1.date),
                  let date2 = dateFormatter.date(from: model2.date) else {
                return false // 如果转换失败则直接返回 false
            }
            return date1 > date2
        }
        let completedCount = sortedModels.filter { !$0.completed && !$0.canceled }.count
        let complete = completedCount > 9 ? String(completedCount) : String(format: "0%ld", completedCount)
        self.pending = complete
        self.invite = String(sortedModels.filter { $0.type == 0 }.count)
        self.invited = String(sortedModels.filter { $0.type == 1 }.count)
        self.dataSource = sortedModels
        onSuccess?(self.dataSource)
    }
}

@objcMembers
class PEMMotionCongruenceModel: PEMModel,Codable {
    var date = ""
    var time = ""
    var location = ""
    var inviter = ""
    var type = 0
    var name = ""
    var month = ""
    var completed = false
    var canceled = false
    var isDefault = true
    override class func getTableName() -> String {
        return  String(describing: PEMMotionCongruenceModel.self)
    }
}
